package org.lib.jpamysql.oto;

import java.util.Objects;

import org.lib.jpamysql.oto.dto.AddBasicDTO;
import org.lib.jpamysql.oto.dto.UpdateNameDTO;
import org.lib.jpamysql.oto.vo.OtoBasicVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value="/otoBasic")
@Slf4j
public class OtoBasicController {

	@Autowired
	private OtoBasicDAO basicDao;

	@Autowired
	private OtoExtDAO extDao;
	
	@Autowired
	private OtoNameDAO nameDao;
	
	/**
	 * 添加
	 * @author ben
	 * @date 2021-10-18 15:38:45 CST
	 * @param dto
	 * @return
	 */
	@PostMapping(value="/add")
	public Long add(@RequestBody AddBasicDTO dto) {
		OtoBasic newBasic = new OtoBasic();
		newBasic.setName(dto.getName());
		
		// 试验1：先添加OtoExt
		// basic_id 为 null
//		if (StringUtils.hasText(dto.getDetail())) {
//			OtoExt ext = new OtoExt();
//			ext.setDetail(dto.getDetail());
//			extDao.save(ext);
//			log.info("ext 1={}", ext);
//		}
		
		basicDao.save(newBasic);
		Long newId = newBasic.getId();

		// 试验2：后添加OtoExt
		// basic_id 为 null
		if (Objects.nonNull(newId) && StringUtils.hasText(dto.getDetail()) ) {
			OtoExt ext = new OtoExt();
			ext.setDetail(dto.getDetail());
			ext.setBasic(newBasic);
			
			extDao.save(ext);
			log.info("ext 2={}", ext);
		}
		
		if (Objects.nonNull(newId)) {
			OtoName otoName = new OtoName();
			otoName.setBasic(newBasic);
			
			nameDao.save(otoName);
			log.info("otoName={}", otoName);
		}
		
		log.info("newBasic={}", newBasic);
		return newBasic.getId();
	}
	
	/**
	 * 查找
	 * @author ben
	 * @date 2021-10-18 15:38:52 CST
	 * @param id
	 * @return
	 */
	@GetMapping(value="/get")
	public OtoBasicVO get(@RequestParam Long id) {
		Objects.requireNonNull(id);
		
		OtoBasic basic = basicDao.findById(id).orElse(null);
		
		// 输出basic导致 java.lang.StackOverflowError！禁止
//		log.info("basic={}", basic);
		
		if (basic == null) {
			return null;
		}
		
		OtoBasicVO vo = new OtoBasicVO();
		BeanUtils.copyProperties(basic, vo);
		
		OtoExt ext = basic.getExt();
		if (ext != null) {
			vo.setDetail(ext.getDetail());
		} else {
			vo.setDetail("");
		}
		
		return vo;
	}
	
	/*
	 * 删除
	 */
	@GetMapping(value="/del")
	public Boolean del(@RequestParam Long id) {
		Objects.requireNonNull(id);
		
		basicDao.deleteById(id);
		
		return basicDao.existsById(id);
	}
	
	@PostMapping(value="/updateName")
	public Boolean updateName(@RequestBody UpdateNameDTO dto) {
		Long id = dto.getId();
		Objects.requireNonNull(id);
		
		OtoBasic basic = basicDao.getById(id);
		if (basic == null) {
			return false;
		}
		
		String nname = dto.getName();
		if (!StringUtils.hasText(nname)) {
			return false;
		}
		
		// 更新
		basic.setName(dto.getName());
		basicDao.save(basic);
		
		return true;
	}
	
}
